package com.kui.web;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kui.entity.Files;
import com.kui.entity.Folder;
import com.kui.entity.User;
import com.kui.service.FilesService;

public class DelShareServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session = request.getSession();
		String fileid = request.getParameter("fileid");
		User user = (User) session.getAttribute("user");
		Folder folder = (Folder) session.getAttribute("folder");
		if(fileid == null || "".equals(fileid)){
			response.sendRedirect("file.jspx?fid=" + folder.getId());
		}else{
			FilesService fs = new FilesService();
			if("admin".equals(user.getRole())){
				Files ff = fs.findById(fileid);
				
				if(ff == null){
					response.sendError(404,"文件未找到");
					return;
				}
				String path = getServletContext().getRealPath("/");
				
				File file = new File(path +"/upload/" +ff.getDownname());
				file.delete();
				fs.delById(fileid);
				return;
			}
			
			Files f = fs.findByIdAndEmpId(fileid,user.getId());
			if(f == null){
				response.sendError(403,"你无权进行此操作或者文件不存在！");
			}else{
				 String path = getServletContext().getRealPath("/");
				
				File file = new File(path +"/upload/" +f.getDownname());
				file.delete();
				fs.delById(fileid);
				
			}
		}
	}

}
